//获取当前时间
var d = new Date();
var m = d.getMonth() + 1;
var currentYear=d.getFullYear();
var currentTime = d.getFullYear() + "-" + m + "-" + d.getDate() + " "
+ d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();// 当前日期
var currentDate = d.getFullYear() + "-" + m + "-" + d.getDate();
//当年1月1日
var startTime=new Date(Date.parse((d.getFullYear()+"-01-01").replace(
		/-/g, "/")));
$(function() {
	$('#f-year').datetimepicker({
		startView: 4,
		minView:4,
		autoclose : true,
		format : 'yyyy'
	});
	$("#f-year").val(currentYear);
	getAllBreakerUnfinishedData();// 获取全网未完成传动的断路器信息
});


//特维分析页
var spStation = {};//所有特维站[name,id]
var isAll=true;//是否是全网查询
var spStationName=[];//特维站点
function getAllBreakerUnfinishedData() {
	$("#date").html(currentDate);
	getAllStations();
	$("#loading").show();
	$("#detailDiv").hide();
	$.getJSON("/rest/specialMaintenace/getAllUnfinishedBreaker/" + currentTime,
			function(result) {
		$("#loading").hide();
		$("#detailDiv").show();
		initAllAnalyseTb(result);
	});
}
function addToSearch(key, value, isTransPinYin) {
	spStationName.push({
		id : key,
		name : value
	});
	if (isTransPinYin) {
		var pinyinkey = pinyinUtil.getFirstLetter(key);
		spStationName.push({
			id : pinyinkey,
			name : value
		});
	};
}
$("#fenxi-btn").click(
		function() {
			var statName = $("#f-subname").val();
			var year = $("#f-year").val();
			if(year==currentYear){
				//如果选择当前年就传今天
				currentTime = d.getFullYear() + "-" + m + "-" + d.getDate() + " "
				+ d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();// 当前日期
				$("#date").html(currentTime);
			}else{
				currentTime=year+"-12-30 23:59:59";
				$("#date").html(year);
			}
			if (statName != "") {
				 isAll=false;//是否是全网查询
				var subId = spStation[statName];
				$("#loading").show();
				$("#detailDiv").hide();
				$.getJSON("/rest/specialMaintenace/getUnfinishedBreakerBySid/"
						+ subId + "/" + currentTime, function(result) {
					$("#loading").hide();
					$("#detailDiv").show();
					initAnalyseTbByStation(statName, result);
				});

			} else {
			    isAll=true;//是否是全网查询
			    $("#loading").show();
			    $("#detailDiv").hide();
				$.getJSON("/rest/specialMaintenace/getAllUnfinishedBreaker/" + currentTime,
						function(result) {
							$("#loading").hide();
							$("#detailDiv").show();
							initAllAnalyseTb(result);
						});
			}
		});
//获取所有的站点
function getAllStations(){
	// 获取全网所有特维站
	$.getJSON("/rest/getStations/500", function(result) {
		for ( var i in result) {
			addToSearch(result[i].name, result[i].name, true);
			spStation[result[i].name]=result[i].id;
		}
		 $("#f-subname").typeahead({
			source : spStationName,
			autoSelect : false
		});
	});
}
// 全网传动分析
function initAllAnalyseTb(data) {
	$("#info").empty();
	$("#unfinished").empty();
	$(
			"<div class='col-sm-2  border-right'><div class='description-block'><p>统计范围</p><p class='text-yellow'>"
					+ "全网"
					+ "</p>"
					+ "</div></div><div class='col-sm-2 border-right'><div class='description-block'><p>特维厂站总数</p>"
					+ "<p class='text-blue'>"
					+ data.totalStation
					+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
					+ "<p>已完成</p><p class='text-green'>"
					+ data.stationFinished
					+ "</p></div></div><div class='col-sm-2 border-right'><div class='description-block'>"
					+ "<p>未完成</p><p class='text-red'>"
					+ data.stationUnfinished
					+ "</p></div></div><div class='col-sm-2  border-right'><div class='description-block'><p>特维设备总数</p>"
					+ "<p class='text-blue'>"
					+ data.totalBreaker
					+ "</p></div></div><div class='col-sm-2 border-right'><div class='description-block'>"
					+ "<p>已完成</p><p class='text-green'>"
					+ data.breakerFinished
					+ "</p></div></div><div class='col-sm-1 border-right'><div class='description-block'>"
					+ "<p>未完成</p><p class='text-red'>"
					+ data.breakerUnfinished
					+ "</p></div></div>").appendTo($("#info"));
	initChuanDongTb(data);
}
var columns = [ "厂站","间隔名称", "开关名称", "A相最后跳闸时间", "B相最后跳闸时间", "C相最后跳闸时间", "计划时间",
                "是否在6.30之前应完成" ];
// 一个站的传动分析
function initAnalyseTbByStation(statName, data) {
	$("#info").empty();
	$("#unfinished").empty();
	$(
			"<div class='col-sm-3 border-right'><div class='description-block'><p>统计范围</p><p class='text-yellow'>"
					+ statName
					+ "</p>"
					+ "</div></div><div class='col-sm-3 border-right'><div class='description-block'><p>需特维设备总数</p>"
					+ "<p class='text-blue'>"
					+ data.total
					+ "</p></div></div><div class='col-sm-3 border-right'><div class='description-block'>"
					+ "<p>已完成</p><p class='text-green'>"
					+ data.finishedNum
					+ "</p></div></div><div class='col-sm-3 border-right'><div class='description-block'>"
					+ "<p>未完成</p><p class='text-red'>"
					+ data.unfinishedNum
					+ "</p></div></div>").appendTo($("#info"));

	initChuanDongTb(data);
}
var finishedTb =null;
function initChuanDongTb(data){
	var opt = MyTable.getDefaultOptions();
	opt.paging=true;
	opt.searching=true;
	opt.lengthChange=true;
	//如果有已完成的
	if(finishedTb==null){
		finishedTb = new MyTable('finishedTb', columns, opt);
	}
	finishedTb.setDataSet(data.finished);
	finishedTb.show();

	// 如果有未按要求时间完成的
	if (data.demand.length != 0) {
		$(
				"<div class='box box-warning'><div class='box-header'><h3 class='box-title'>未按要求时间完成</h3><div class='box-tools pull-right'>"
						+ "<button type='button' class='btn btn-box-tool' data-widget='collapse'><i class='fa fa-minus'></i></button>"
						+ "</div></div><div class='box-body'>"
						+ "<table id='demand' style='width: 100%;' class='table table-bordered table-hover'>"
						+ "</table></div></div>").appendTo($("#unfinished"));
		var strTime = d.getFullYear() + "-06-30 00:00:00";
		var demandTime = new Date(Date.parse(strTime.replace(/-/g, "/")));
		opt.columnDefs=[ {
		       "targets": [3,4,5],
		       "createdCell": function (td, cellData, rowData, row, col) {
		    	   	 var time = new Date(Date.parse((cellData).replace(
							/-/g, "/")));
			         if (time<startTime||time > demandTime) {
			            $(td).css('color', 'red');
			         }
		       	  }
		     } ];
		var demandTb = new MyTable('demand', columns, opt);
		demandTb.setDataSet(data.demand);
		demandTb.show();
	}
	// 如果有截止目前未完成，但未到要求时间或者没有要求时间
	if (data.plan.length != 0) {
		$(
				"<div class='box box-warning'><div class='box-header'><h3 class='box-title'>截止目前未完成</h3><div class='box-tools pull-right'>"
						+ "<button type='button' class='btn btn-box-tool' data-widget='collapse'><i class='fa fa-minus'></i></button>"
						+ "</div></div><div class='box-body'>"
						+ "<table id='current' style='width: 100%;' class='table table-bordered table-hover'>"
						+ "</table></div></div>").appendTo($("#unfinished"));
		opt.columnDefs=[ {
		       "targets": [3,4,5],
		       "createdCell": function (td, cellData, rowData, row, col) {
		    	   	 var time = new Date(Date.parse((cellData).replace(
							/-/g, "/")));
			         if (time<startTime||time > currentTime) {
			            $(td).css('color', 'red');
			         }
		       	  }
		 } ];
		var planTb = new MyTable('current', columns, opt);
		planTb.setDataSet(data.plan);
		planTb.show();
	}
}